/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.model;

import java.util.Date;

import org.geoscheduler.commons.marshaller.JSONField;

/**
 * Accounts registered in our database
 * 
 * @author paja
 * @author lko
 * @version $Id: Account.java 196 2008-04-13 16:05:01Z lko $
 */
public class Account extends VersionedEntity implements Identifiable<Long> {

	public enum AccountType {
		INTERNAL, 
		GOOGLE_ACCOUNT;
	}

	private Long id;
	private String login;
	private String passwd;
	private int accountType;
	private Date created;
	private Date accessed;

	public Account() {
		super();
	}

	public Account(String login, String passwd, AccountType accountType) {
		this(null, login, passwd, accountType, new Date(), null);
	}

	public Account(Long id, String login, String passwd, AccountType accountType, Date created, Date accessed) {
		super();
		this.id = id;
		this.login = login;
		this.passwd = passwd;
		this.accountType = accountType == null ? 0 : accountType.ordinal();
		this.created = created;
		this.accessed = accessed;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((accessed == null) ? 0 : accessed.hashCode());
		result = prime * result + accountType;
		result = prime * result + ((created == null) ? 0 : created.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((passwd == null) ? 0 : passwd.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		
		if (!super.equals(obj)) {
			return false;
		}
		
		if (getClass() != obj.getClass()) {
			return false;
		}
		
		final Account other = (Account) obj;
		
		if (accessed == null) {
			if (other.accessed != null) {
				return false;
			}
		} else if (!accessed.equals(other.accessed)) {
			return false;
		}
		
		if (created == null) {
			if (other.created != null) {
				return false;
			}
		} else if (!created.equals(other.created)) {
			return false;
		}
		
		if (accountType != other.accountType) {
			return false;
		}
		
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		
		if (login == null) {
			if (other.login != null) {
				return false;
			}
		} else if (!login.equals(other.login)) {
			return false;
		}
		
		if (passwd == null) {
			if (other.passwd != null) {
				return false;
			}
		} else if (!passwd.equals(other.passwd)) {
			return false;
		}
		
		return true;
	}
	
	
// Getters & Setters

	@JSONField
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@JSONField
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	@JSONField
	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public int getAccountType() {
		return accountType;
	}

	@JSONField(defaultValue="INTERNAL")
	public AccountType getAccountTypeEnum() {
		return AccountType.values()[accountType];
	}

	public void setAccountTypeEnum(AccountType accountType) {
		this.accountType = accountType.ordinal();
	}

	public void setAccountType(int accountType) {
		this.accountType = accountType;
	}

	@JSONField
	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	@JSONField
	public Date getAccessed() {
		return accessed;
	}

	public void setAccessed(Date accessed) {
		this.accessed = accessed;
	}
}